package com.minjiang.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.minjiang.blog.mode.Blog;
import com.minjiang.blog.queryvo.BlogQuery;
import com.minjiang.blog.queryvo.SearchBlog;
import com.minjiang.blog.service.BlogService;
import com.minjiang.blog.service.TypeService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@SuppressWarnings("ALL")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    //查询博客列表
    @RequestMapping(value = "/blogs",method = RequestMethod.GET)
    public String blogs(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum){
        //按照排序字段 倒序 排序
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum,10,orderBy);
        List<BlogQuery> allBlog = blogService.getAllBlog();
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(allBlog);
        //搜索框
        model.addAttribute("typeAll",typeService.findAllType());
        //列表
        model.addAttribute("pageInfo",pageInfo);
        return "admin/blogs";
    }

    //    搜索框
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public String searchBlog(Model model, SearchBlog searchBlog,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        List<BlogQuery> blogBySearch = blogService.getBlogBySearch(searchBlog);
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogBySearch);
        model.addAttribute("pageInfo",pageInfo);
        return "/admin/blogs";
    }
    //转跳新增页面(每次转跳刷新页面）
    @RequestMapping(value = "/blogs/add",method = RequestMethod.GET)
    public String blogsAdd(Model model){
        model.addAttribute("types",typeService.findAllType());
        model.addAttribute("blog",new Blog());
        return "admin/blogs-add";
    }

    //新增
    @RequestMapping(value = "/blogs/input",method = RequestMethod.POST)
    public String blogsIput(Blog blog, RedirectAttributes attributes){
        if (blogService.getBlogByName(blog.getTitle()) == null){
            attributes.addFlashAttribute("message","新增失败");
        }
        attributes.addFlashAttribute("message","新增成功");
        blog.setType(typeService.findType(blog.getType().getId()));
        blogService.saveBlog(blog);
        return "redirect:/admin/blogs";
    }
    //    删除
    @RequestMapping(value = "/blogs/delete/{id}",method = RequestMethod.GET)
    public String deleteBlog(@PathVariable Long id, RedirectAttributes attributes){
        System.out.println("这里有开始执行"+id);
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }



    //----------------------------------------------------------------------------------------------------------
    //跳转到编辑页面
    @RequestMapping(value = "/blogs/update/{id}",method = RequestMethod.GET)
    public String blogsUpdate(@PathVariable Long id, Model model){
        Blog blogById = blogService.getBlogById(id);
        model.addAttribute("types",typeService.findAllType());
        model.addAttribute("blogs",blogById);
        return "admin/blogs-update";
    }

    //修改
    @RequestMapping(value = "/blogs/edit/{id}",method = RequestMethod.POST)
    public String blogsEdit(@Valid Blog blog, RedirectAttributes redirectAttributes){
        int i = blogService.updateBlog(blog);
        if (i >0){
            redirectAttributes.addFlashAttribute("message","编辑成功");
        }else {
            redirectAttributes.addFlashAttribute("message","编辑失败");
        }
        return "redirect:/admin/blogs";
    }
}

